home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / File / Search & Replace Names < prev    next >
Encoding:
Text File  |  1999-03-04  |  4.3 KB  |  209 lines  |  [TEXT/ToyS]

  1. -- Properties
  2. property kasPrefName : "S&R Names V1.0"
  3.  
  4. -- Globals
  5. global gasInfoWind -- Info window
  6. global gasInfoPos -- Position of info window
  7.  
  8. global gasFoldersToDo -- The folders left to process
  9. global gasChecked -- Number checked!
  10. global gasChanged -- Number changed
  11.  
  12. global gasSearch -- Search string
  13. global gasReplace -- Replace with
  14. global gasDoRsrc, gasDoData
  15.  
  16.  
  17. on open fsObjs
  18.     -- Load prefs, show window
  19.     pfLoad()
  20.     
  21.     -- Set up prefix
  22.     set gasChecked to 0
  23.     set gasChanged to 0
  24.     
  25.     GetInputs()
  26.     
  27.     set gasInfoWind to display info titled kasPrefName ¬
  28.         located at gasInfoPos ¬
  29.         message "Scanning…"
  30.     
  31.     display info gasInfoWind ¬
  32.         message ("Search: " & gasSearch) ¬
  33.         at line 8
  34.     
  35.     display info gasInfoWind ¬
  36.         message ("Replace: " & gasReplace) ¬
  37.         at line 9
  38.     
  39.     -- Do files
  40.     set gasFoldersToDo to {}
  41.     
  42.     repeat with fsObj in fsObjs
  43.         set myInfo to (basic info for fsObj)
  44.         
  45.         if (system type of myInfo is "fold") then
  46.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  47.         else
  48.             DoOne(fsObj)
  49.         end if
  50.     end repeat
  51.     
  52.     -- Do folders
  53.     repeat while gasFoldersToDo is not {}
  54.         -- Pop one off the end
  55.         set n to the number of items of gasFoldersToDo
  56.         set fsObj to item n of gasFoldersToDo
  57.         
  58.         if (n > 1) then
  59.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  60.         else
  61.             set gasFoldersToDo to {}
  62.         end if
  63.         
  64.         display info gasInfoWind ¬
  65.             message ("Folders to go: " & n) ¬
  66.             at line 6 ¬
  67.             using color (15 * 32)
  68.         
  69.         -- Process it
  70.         DoOne(fsObj)
  71.         GoDeep(fsObj)
  72.     end repeat
  73.     
  74.     display info gasInfoWind message "DONE!"
  75.     
  76.     pause for 3 with seconds timing -- Let screen wait...
  77.     
  78.     set gasInfoPos to screen location of ¬
  79.         (display info gasInfoWind with disposal)
  80.     
  81.     pfSave() -- Save window location
  82. end open
  83.  
  84.  
  85. on GetInputs()
  86.     set gasSearch to GetString("search string", gasSearch)
  87.     set gasReplace to GetString("replace string", gasReplace)
  88. end GetInputs
  89.  
  90.  
  91. on GetString(sName, sDef)
  92.     return text returned of ¬
  93.         (display dialog ("Enter the " & sName & ":") ¬
  94.             buttons {"Cancel", "OK"} ¬
  95.             default answer sDef ¬
  96.             default button 2 ¬
  97.             with icon note)
  98. end GetString
  99.  
  100.  
  101. on ShowAlert(msg)
  102.     return button returned of ¬
  103.         (display dialog msg ¬
  104.             buttons {"Cancel", "Yo!"} ¬
  105.             default button 2 ¬
  106.             with icon stop)
  107. end ShowAlert
  108.  
  109.  
  110. on ShowChoice(msg, choices)
  111.     return button returned of ¬
  112.         (display dialog msg ¬
  113.             buttons choices ¬
  114.             default button (number of items of choices) ¬
  115.             with icon caution)
  116. end ShowChoice
  117.  
  118.  
  119. on DoOne(fsObj)
  120.     set fInfo to (basic info for fsObj)
  121.     set fname to (catalog name of fInfo)
  122.     
  123.     set gasChecked to gasChecked + 1
  124.     
  125.     display info gasInfoWind ¬
  126.         message fname ¬
  127.         at line 2
  128.     
  129.     set newName to munge fname ¬
  130.         searching for gasSearch ¬
  131.         replacing it with gasReplace
  132.     
  133.     if (newName is not fname) then
  134.         display info gasInfoWind ¬
  135.             message ("Renaming") ¬
  136.             at line 4
  137.         try
  138.             collate fsObj ¬
  139.                 renaming it to newName
  140.         on error err
  141.             display info gasInfoWind ¬
  142.                 message ("Error: " & err) ¬
  143.                 at line 15
  144.         end try
  145.         set gasChanged to gasChanged + 1
  146.     end if
  147.     
  148.     display info gasInfoWind ¬
  149.         message ("Checked: " & gasChecked) ¬
  150.         at line 12
  151.     display info gasInfoWind ¬
  152.         message ("Changed: " & gasChanged) ¬
  153.         at line 13
  154. end DoOne
  155.  
  156.  
  157. on GoDeep(foldObj)
  158.     display info gasInfoWind ¬
  159.         message "Path: " & (foldObj as string)
  160.     
  161.     set daddy to foldObj as string
  162.     
  163.     -- Do kinds that match
  164.     display info gasInfoWind ¬
  165.         message "Scanning files" at line 5
  166.     
  167.     set myItems to the entries in foldObj ¬
  168.         whose kinds are a file
  169.     
  170.     repeat with myItem in myItems
  171.         DoOne((daddy & myItem) as alias)
  172.     end repeat
  173.     
  174.     -- Do folders
  175.     display info gasInfoWind ¬
  176.         message "Scanning subfolders" at line 5
  177.     
  178.     set myItems to the entries in foldObj ¬
  179.         whose kinds are a folder
  180.     
  181.     repeat with myItem in myItems
  182.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  183.     end repeat
  184.     
  185.     -- Done
  186.     display info gasInfoWind ¬
  187.         message "…" at line 5
  188. end GoDeep
  189.  
  190.  
  191. on pfLoad()
  192.     try
  193.         set ourPrefs to (load preference named kasPrefName)
  194.     on error
  195.         set ourPrefs to {{8, 48}, ">LORE<", "◊LIFE◊", true, true}
  196.     end try
  197.     
  198.     set gasInfoPos to item 1 of ourPrefs
  199.     set gasSearch to item 2 of ourPrefs
  200.     set gasReplace to item 3 of ourPrefs
  201.     set gasDoData to item 4 of ourPrefs
  202.     set gasDoRsrc to item 5 of ourPrefs
  203. end pfLoad
  204.  
  205.  
  206. on pfSave()
  207.     save preference {gasInfoPos, gasSearch, gasReplace, gasDoData, gasDoRsrc} named kasPrefName
  208. end pfSave
  209.